package com.rz.paas.test.inChannel.viewModel

import android.app.Application
import android.os.Looper
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.rz.paas.test.constant.AppConstant

class CommunicationViewModel(application: Application) : AndroidViewModel(application) {
    private var communicationMode = CommunicationMode.COMMUNICATION_MODE
    fun setCommunicationMode(mode: Int) {
        communicationMode = mode
    }

    fun getCommunicationMode(): Int {
        return communicationMode
    }

    private val _renderMode = MutableLiveData(0)
    val renderMode: LiveData<Int>
        get() = _renderMode

    fun changeRenderMode(renderMode: Int) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _renderMode.value = renderMode
        } else {
            _renderMode.postValue(renderMode)
        }
    }


    private val _encodeResolution = MutableLiveData(0)
    val encodeResolution: LiveData<Int>
        get() = _renderMode

    fun changeEncodeResolution(encodeResolution: Int) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _encodeResolution.value = encodeResolution
        } else {
            _encodeResolution.postValue(encodeResolution)
        }
    }


    private val _encodeFPS = MutableLiveData(0)
    val encodeFPS: LiveData<Int>
        get() = _encodeFPS

    fun changeEncodeFPS(encodeFPS: Int) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _encodeFPS.value = encodeFPS
        } else {
            _encodeFPS.postValue(encodeFPS)
        }
    }


    private val _encodeDirection = MutableLiveData(0)
    val encodeDirection: LiveData<Int>
        get() = _encodeDirection

    fun changeEncodeDirection(encodeDirection: Int) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _encodeDirection.value = encodeDirection
        } else {
            _encodeDirection.postValue(encodeDirection)
        }
    }


    fun optionIndex(optionId: Int, selectedIndex: Int) {
        when (optionId) {
            AppConstant.ENCODE_DIRECTION_DATA_INDEX ->
                changeEncodeDirection(selectedIndex)

            AppConstant.ENCODE_FPS_DATA_INDEX ->
                changeEncodeFPS(selectedIndex)

            AppConstant.ENCODE_RESOLUTION_DATA_INDEX ->
                changeEncodeResolution(selectedIndex)

            AppConstant.RENDER_MODE_INDEX ->
                changeRenderMode(selectedIndex)
        }
    }


    fun finishActivity() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _finishActivity.value = true
        } else {
            _finishActivity.postValue(true)
        }
    }

    private val _finishActivity = MutableLiveData(false)
    val finishActivity: LiveData<Boolean>
        get() = _finishActivity


}